package com.yonyou.isv.demo.openapi.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @description: 接口实例化
 * @author: nishch
 * @create: 2021-07-08
 **/
@Slf4j
public class ApiBaseContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    public static <T> T getBeanInstance(Class<T> clazz){
        try{
            return applicationContext.getBean(clazz);
        }catch (Exception e){
            log.warn("Bean is not exist for class:{}", clazz);
            return null;
        }
    }

}
